////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// The MIT License (MIT)                                                      //
//                                                                            //
// Copyright (c) 2013 Charles Teague                                          //
//                                                                            //
// Permission is hereby granted, free of charge, to any person obtaining a    //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation  //
// the rights to use, copy, modify, merge, publish, distribute, sublicense,   //
// and/or sell copies of the Software, and to permit persons to whom the      //
// Software is furnished to do so, subject to the following conditions:       //
//                                                                            //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software.                        //
//                                                                            //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        //
// DEALINGS IN THE SOFTWARE.                                                  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using RPSDAL;
using RPSDAL.CollectionClasses;
using RPSDAL.HelperClasses;
using RPSDAL.EntityClasses;

using SD.LLBLGen.Pro.ORMSupportClasses;


public partial class CallFinishedPageAdministration : System.Web.UI.Page
{
   
    private void FillControls() 
    {
        for (int i = 0; i < grdPageFields.Rows.Count; i++)
        {
            try
            {
                switch (Convert.ToInt32(grdPageFields.Rows[i].Cells[1].Text))
                {
                    case Parameters.TEXTBOX_TYPE:
                        //textbox
                        TextBox txtTextBox = new TextBox();
                        grdPageFields.Rows[i].Cells[1].Controls.Add(txtTextBox);
                        break;
                    case Parameters.COMBOBOBOX_TYPE:
                        // combobox
                        CallFinishedDropdownFieldValueCollection cComboboxValues = new CallFinishedDropdownFieldValueCollection();
                        IPredicateExpression filter = new PredicateExpression();
                        filter.Add(CallFinishedDropdownFieldValueFields.CallPageFinishedFieldId == Convert.ToInt32(((TextBox)grdPageFields.Rows[i].Cells[5].FindControl("id")).Text));
                        cComboboxValues.GetMulti(filter);

                        DropDownList cmbComboBox = new DropDownList();
                        cmbComboBox.DataSource = cComboboxValues;
                        cmbComboBox.DataValueField = CallFinishedDropdownFieldValueFields.Value.Name;
                        cmbComboBox.DataTextField = CallFinishedDropdownFieldValueFields.Value.Name;
                        cmbComboBox.DataBind();

                        grdPageFields.Rows[i].Cells[1].Controls.Add(cmbComboBox);
                        break;
                    case Parameters.CHECKBOX_TYPE:
                        CheckBox ckCehckBox = new CheckBox();
                        grdPageFields.Rows[i].Cells[1].Controls.Add(ckCehckBox);
                        break;
                    case Parameters.TEXTAREA_TYPE:
                        TextBox txtTextArea = new TextBox();
                        txtTextArea.TextMode = TextBoxMode.MultiLine;
                        grdPageFields.Rows[i].Cells[1].Controls.Add(txtTextArea);
                        break;
                    case Parameters.DATETIME_TYPE:
                        TextBox txtDateTime = new TextBox();
                        txtDateTime.ID = "txtDateTimeId";
                        grdPageFields.Rows[i].Cells[1].Controls.Add(txtDateTime);
                        Image imgDateTime = new Image();
                        imgDateTime.ImageUrl = "jscripts/date/calendar.gif";
                        imgDateTime.ID = "dateId";
                        grdPageFields.Rows[i].Cells[1].Controls.Add(imgDateTime);
                        break;
                }
            }
            catch { }

        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) 
        {
            if (Parameters.LoggedUser != -1) 
            {
                UserEntity eUser = new UserEntity(Parameters.LoggedUser);
                RoleEntity eRole = new RoleEntity(eUser.RoleId);

                if (!eRole.HasConfigureCallFinishedPage) 
                {
                    Parameters.LoggedUser = -1;
                    Response.Redirect("Default.aspx");
                    return;
                }
            }

            CallFinishedPageFieldCollection cCallFinishedPageFields = new CallFinishedPageFieldCollection();
            cCallFinishedPageFields.GetMulti(null,0,new SortExpression(new SortClause(CallFinishedPageFieldFields.OrderIndex,SortOperator.Ascending)));

            grdPageFields.DataSource = cCallFinishedPageFields;
            grdPageFields.DataBind();

            FillControls();
        }
    }

    #region methods
    

    protected void AddNewField(object sender, EventArgs e)
    {
        Global.UserLogSave(Parameters.LoggedUser, "Add New Filed Call Finished Page");
        Response.Redirect("CallFinishedPageField.aspx");
    }
    protected void RemoveField(object sender, EventArgs e)
    {
        Global.UserLogSave(Parameters.LoggedUser, "Remove Fileds Call Finished Page");
        for (int i = 0; i < grdPageFields.Rows.Count; i++) 
        {
            if (((CheckBox)grdPageFields.Rows[i].Cells[4].FindControl("ckRemove")).Checked) 
            {
                int id = 0;
                if (Int32.TryParse(((TextBox)grdPageFields.Rows[i].Cells[5].FindControl("id")).Text, out id))
                {
                    CallFinishedDropdownFieldValueCollection cCallFinishedDropdownFieldValues = new CallFinishedDropdownFieldValueCollection();
                    IPredicateExpression filter = new PredicateExpression();
                    filter.Add(CallFinishedDropdownFieldValueFields.CallPageFinishedFieldId == id);
                    cCallFinishedDropdownFieldValues.DeleteMulti(filter);

                    CallFinishedPageFieldEntity eCallFinishedPageField = new CallFinishedPageFieldEntity(id);
                    eCallFinishedPageField.Delete();

                    Global.UserLogSave(Parameters.LoggedUser, "Remove Filed Call Finished Page id =" + id.ToString());

                }
            }
        }

        Response.Redirect("CallFinishedPageAdministration.aspx");
    }
    protected void ModifyFieldOrder(object sender, EventArgs e)
    {
        Global.UserLogSave(Parameters.LoggedUser, "Modify Filed Order Call Finished Page");
        for (int i = 0; i < grdPageFields.Rows.Count; i++) 
        {
            int id = 0;
            int orderIndex = 0;

            if (Int32.TryParse(((TextBox)grdPageFields.Rows[i].Cells[2].FindControl("txtOrder")).Text, out orderIndex)
                && Int32.TryParse(((TextBox)grdPageFields.Rows[i].Cells[5].FindControl("id")).Text, out id)) 
            {
                CallFinishedPageFieldEntity eCallFinishedPageField = new CallFinishedPageFieldEntity(id);
                if (eCallFinishedPageField.OrderIndex != orderIndex) 
                {
                    eCallFinishedPageField.OrderIndex = orderIndex;
                    eCallFinishedPageField.Save();
                }
            }
        }

        CallFinishedPageFieldCollection cCallFinishedPageFields = new CallFinishedPageFieldCollection();
        cCallFinishedPageFields.GetMulti(null,0,new SortExpression(new SortClause(CallFinishedPageFieldFields.OrderIndex,SortOperator.Ascending)));


        for (int i = 0; i < cCallFinishedPageFields.Count; i++)
        {
            cCallFinishedPageFields[i].OrderIndex = i+1;
            cCallFinishedPageFields[i].Save();
        }

        Response.Redirect("CallFinishedPageAdministration.aspx");
    }
    #endregion methods


    protected void RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (((System.Web.UI.WebControls.Button)(e.CommandSource)).Text == "Edit")
        {
            Global.UserLogSave(Parameters.LoggedUser, "Edit Filed Call Finished Page id="+e.CommandArgument.ToString());
            Response.Redirect("CallFinishedPageField.aspx?id=" + Global.EncryptString(e.CommandArgument.ToString()));
        }        
    }
}
